// pages/partner/consumeHistory/consumeHistory.js
var app = getApp();
var pagea = 1;
var pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consumeHistoryList: [],//列表
    hasMore: true, //是否有更多
    is_load: false,//是否加载
    loading: false,
    equipment_number: '',
    allNum:'',
    isSearch:false,//是否打开搜索
    start_time: '',//开始时间
    end_time: '',//结束时间
    waterCardNum: '',//水卡卡号
    payIndex:0,//支付类型
    payList: [{ id: '', name: '请选择支付类型' }, { id: 1, name: '微信支付' }, { id: 2, name: '实体卡支付' }, { id: 3, name: '电子卡支付' },
      { id: 4, name: '积分支付' }, { id: 5, name: '投币支付' }, { id: 6, name: '支付宝支付'}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.equipment_number) {
      this.setData({
        equipment_number: options.equipment_number
      })
    }
  },
  /*** 生命周期函数--监听页面显示*/
  onShow: function () {
    pagea = 1;
    this.setData({
      consumeHistoryList: [],
      is_load: false,
      hasMore: true,
    })
    this.getconsumptions();//获取水卡列表
  },
  /*** 页面相关事件处理函数--监听用户下拉动作*/
  onPullDownRefresh: function () {
    var that = this;
    pagea = 1;
    that.setData({
      consumeHistoryList: [], // 订单列表
      hasMore: true, // 没有更多了状态
      is_load: false
    })
    that.getconsumptions(); // 获取订单列表
  },

  /*** 页面上拉触底事件的处理函数---(修改为scroll-view中的上拉)*/
  reachBottom: function () {
    var that = this;
    pagea += 1;
    // 当没有数据时,不再请求
    if (!that.data.hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    that.getconsumptions(); // 获取订单列表
  },
  // 获取订单列表
  getconsumptions: function () {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'Partner/Equipment/consumption',
      data: {
        card: that.data.waterCardNum,
        start_time: that.data.start_time,
        end_time: that.data.end_time,
        pay_type: that.data.payList[that.data.payIndex].id,
        page: pagea,
        pageSize: pageSize,
        equipment_number: this.data.equipment_number,
      },
      success: function (res) {
        wx.hideLoading();
        var newlist = '';//newlist(过渡)
        var oldlist = that.data.consumeHistoryList;//原来的数组
        if (res.data.code == 1000) {
          if (oldlist.length <= 0) {
            newlist = res.data.data.order
          } else {
            newlist = oldlist.concat(res.data.data.order);
          }
          if (res.data.data.count <= newlist.length) {
            that.setData({
              hasMore: false
            })
          }
          console.log('最后的数据', newlist)
          that.setData({
            consumeHistoryList: newlist,
            allNum: res.data.data.census.money
          })
        } else {
          that.setData({
            hasMore: false
          })
        }
        //是否加载
        that.setData({
          is_load: true,
          loading: false,
        })

        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  //打开查询
  search(e) {
    let isSearch = e.detail.isSearch;
    var that = this;
    that.setData({
      isSearch: isSearch
    })
  },
  //选择开始时间
  startTimeChange(e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  //选择结束时间
  endTimeChange(e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  //设备名称
  waterCardsInput(e) {
    this.setData({
      waterCardNum: e.detail.value
    })
  },
  //选择支付类型
  statusChange(e) {
    this.setData({
      payIndex: e.detail.value
    })
  },
  //点击搜索
  goSearch() {
    var that = this;
    pagea=1;
    that.setData({
      isSearch: false,
      consumeHistoryList: [],
      is_load: false,
      hasMore: true,
    })
    wx.showLoading();
    that.getconsumptions();
  },
  //返回搜索
  goBack() {
    var that = this;
    that.setData({
      isSearch: false
    })
  },
})